package view.microgui
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import model.macro.Unit;
	import model.micro.Battle;
	import model.micro.FieldUnit;
	import util.MyMath;
	
	/**
	 */
	public class FieldUnitList extends Sprite
	{
		
		static private const MAX_LIST_VISIBLE:int = 5;
		
		public const PADDING:int = 4; // padding between items in list
		private var battle:Battle;
		private var items:Vector.<FieldUnitListItem>;
		private var scroll_index:int;
		private var scroll_y:Number;
		private var scroll_bar:Shape;
		private var active_items:int;
		
		public function FieldUnitList(unit_list:Vector.<Unit> = null)
		{
			active_items = 0;
			scroll_bar = new Shape();
			items = new Vector.<FieldUnitListItem>();
			if (unit_list != null)
			{
				for each (var unit:Unit in unit_list)
				{
					addUnit(unit);
				}
			}
			
			scroll_index = 0;
			scroll_y = 0;
			scroll_bar.graphics.beginFill(0x444444);
			scroll_bar.graphics.drawRect(0, 0, 4, (FieldUnitListItem.HEIGHT + PADDING) * MAX_LIST_VISIBLE);
			scroll_bar.graphics.endFill();
			scroll_bar.x = FieldUnitListItem.WIDTH + PADDING;
			scroll_bar.alpha = 0.7;
			resizeScrollbar();
			addChild(scroll_bar);
			
			addEventListener(MouseEvent.MOUSE_WHEEL, scroll);
		
		}
		
		private function scroll(e:MouseEvent):void
		{
			scroll_index = MyMath.max2(MyMath.min2(scroll_index - MyMath.sign(e.delta), active_items - MAX_LIST_VISIBLE), 0);
		}
		
		public function addUnit(unit:Unit):void
		{
			if (unit == null)
			{
				throw new ArgumentError("unit is null");
			}
			var item:FieldUnitListItem = new FieldUnitListItem(unit);
			items.push(item);
			addChild(item);
			
			if (item.filter)
			{
				active_items++;
				resizeScrollbar();
			}
		}
		
		public function removeUnit(unit:Unit):void
		{
			for (var i:int = items.length - 1; i >= 0; i--)
			{
				if (items[i].unit == unit)
				{
					if (items[i].filter)
					{
						active_items--;
						resizeScrollbar();
						scroll_index = MyMath.max2(MyMath.min2(scroll_index, active_items - MAX_LIST_VISIBLE), 0);
					}
					removeChild(items[i]);
					items.splice(i, 1);
				}
			}
		}
		
		private function resizeScrollbar():void
		{
			scroll_bar.scaleY = MyMath.min2(MAX_LIST_VISIBLE / (1.0 * active_items), MyMath.min2(active_items / MAX_LIST_VISIBLE, 1.0));
		}
		
		public function toggleVisible():void
		{
			visible = !visible;
		}
		
		public function update():void
		{
			if (Math.abs(scroll_y - scroll_index) > 0.01)
			{
				scroll_y = (2 * scroll_y + scroll_index) / 3;
				scroll_bar.y = ((scroll_y / MyMath.max2((active_items - MAX_LIST_VISIBLE), 1.0))) * (MAX_LIST_VISIBLE * (FieldUnitListItem.HEIGHT + PADDING) - scroll_bar.height);
			}
			
			var list_index:int = 0;
			for (var i:int; i < items.length; i++)
			{
				var item:FieldUnitListItem = items[i];
				if (list_index >= MAX_LIST_VISIBLE + scroll_index)
				{
					break;
				}
				if (item.filter)
				{
					item.y = (list_index - scroll_y) * (items[i].height + PADDING);
					list_index++;
					item.visible = (list_index >= scroll_index);
				}
				else
				{
					item.visible = false;
				}
			}
			for (; i < items.length; i++)
			{
				item = items[i];
				item.visible = false;
			}
			
			for each (item in items)
			{
				item.update();
			}
		}
	
	}

}